/*
 * @Author: jiu yin
 * @Date: 2025-03-05 14:41:02
 * @LastEditTime: 2025-08-21 15:52:17
 * @LastEditors: jiu yin zhen jing
 * @FilePath: \one-half-vue3-2025-3-7\vite.config.ts
 */
import { fileURLToPath, URL } from "node:url";

import vue from "@vitejs/plugin-vue";
import { codeInspectorPlugin } from "code-inspector-plugin";
import ip from "ip";
import UnoCSS from "unocss/vite";
import AutoImport from "unplugin-auto-import/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import Components from "unplugin-vue-components/vite";
import { defineConfig } from "vite";
import vueDevTools from "vite-plugin-vue-devtools";
import { PlusProComponentsResolver } from "@plus-pro-components/resolver";
import vueJsx from "@vitejs/plugin-vue-jsx";
import importToCDN from "vite-plugin-cdn-import";

// https://vite.dev/config/
export default defineConfig({
   plugins: [
      vue(),
      vueJsx(),
      vueDevTools(),
      UnoCSS(),
      codeInspectorPlugin({ bundler: "vite" }),

      AutoImport({
         imports: ["vue", "vue-router"],
         resolvers: [ElementPlusResolver()],
         dts: "src/types/auto-imports.d.ts",
      }),
      Components({
         dts: "src/types/components.d.ts",
         resolvers: [ElementPlusResolver(), PlusProComponentsResolver()],
      }),

      importToCDN({
         enableInDevMode: true, //开发环境是否启用
         modules: [
            {
               name: "axios",
               var: "axios",
               path: "https://resource.ym2meta.com/public/axios.min.js",
            },
            {
               name: "echarts",
               var: "echarts",
               path: "https://resource.ym2meta.com/public/echarts.min.js",
            },
         ],
      }),
   ],
   css: {
      preprocessorOptions: {
         scss: {
            api: "modern-compiler", // or "modern"
         },
      },
   },
   resolve: {
      alias: {
         "@": fileURLToPath(new URL("./src", import.meta.url)),
      },
      //extensions数组的意思是在import组件的时候自动补全.vue等文件后缀
      extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"],
   },
   server: {
      open: true,
      port: 9000,
      host: ip.address(),
   },
   optimizeDeps: {
      // 预构建依赖
      include: ["vue", "vue-router", "axios",],
      // 排除不需要预构建的依赖
      exclude: [],
   },
   build: {
      minify: "esbuild",
      terserOptions: {
         compress: {
            //  parallel: true, // 开启多线程压缩
            //生产环境时移除console
            drop_console: true,
            drop_debugger: true,
         },
      },
      // cacheDir: "node_modules/.vite", //隐藏缓存的文件
      cssCodeSplit: true, // 开启 CSS 代码分割
      rollupOptions: {
         plugins: [
            // visualizer({
            //    open: true, //依赖包可视化
            //    filename: "./sizeAnalyze.html", // 生成的分析报告文件名
            //    gzipSize: true, // 收集 gzip 大小并显示
            //    brotliSize: true // 收集 brotli 大小并显示
            // })
         ],
         output: {
            manualChunks(id) {
               if (id.includes("node_modules") && (id.endsWith(".js") || id.endsWith(".ts"))) {
                  return "vendor";
               }
            },
         },

         cache: true, // 启用 Rollup 缓存
         treeshake: {
            preset: "recommended", // 使用推荐预设

            // 模块副作用配置
            moduleSideEffects: true, // 或 false，或字符串数组

            // 属性读取的副作用
            propertyReadSideEffects: true,

            // 未知的全局变量
            unknownGlobalSideEffects: false,

            // 标记纯函数
            annotations: true,

            // 优化 try-catch
            tryCatchDeoptimization: false,
         },

         external: ["vue", "vue-router", "axios",],
      },
   },
   envDir: "./env",
});
